(function($) {
    $.fn.lazyLoad = function(options) {
        var defaults = {
            attr: "data-url",
            container: $(window),
            callback: $.noop
        };
        var params = $.extend({}, defaults, options || {});
        params.cache = [];
        $(this).each(function() {
            var node = this.nodeName.toLowerCase(),
                url = $(this).attr(params["attr"]);
            var data = {
                obj: $(this),
                tag: node,
                url: url
            };
            params.cache.push(data);
        });
        var callback = function(call) {
            if ($.isFunction(params.callback)) {
                params.callback.call(call.get(0));
            }
        };
        var loading = function() {
            var contHeight = params.container.height();
            if ($(window).get(0) === window) {
                contop = $(window).scrollTop();
            } else {
                contop = params.container.offset().top;
            }
            $.each(params.cache, function(i, data) {
                var o = data.obj,
                    tag = data.tag,
                    url = data.url,
                    post, posb;
                if (o) {
                    post = o.offset().top - contop
                    posb = post + o.height()
                    if (post >= 0 && post < contHeight || posb > 0 && posb <= contHeight) {
                        if (url) {
                            if (tag === "img") {
                                callback(o.attr("src", url));
                            }
                            if (tag === "span") {
                                o[0].style.backgroundImage = "url(" + url + ")";
                            } else {
                                o.load(url, {}, function() {
                                    callback(o);
                                });
                            }
                        } else {
                            callback(o);
                        }
                        data.obj = null;
                    }
                }
            });
        };
        loading();
        window.loadImag = loading;
        params.container.bind("scroll", loading);
    };
})(jQuery);
(function(window) {
    var D = window.document,
        BODY = D.body,
        HEAD = D.head,
        HTML = D.getElementsByTagName("html")[0];

    function getDpi() {
        return window.devicePixelRatio;
    }

    function getViewWidth() {
        return window.document.documentElement.clientWidth;
    }

    function getViewHeight() {
        return window.document.documentElement.clientHeight;
    }
    var setPageView = function setPageView() {
        var dpi = getDpi(),
            iscale = 1 / dpi,
            viewport = D.querySelector('meta[name="viewport"]');
        if (!viewport) {
            viewport = D.createElement("meta");
            viewport.setAttribute("name", "viewport");
            HEAD.insertBefore(viewport, HEAD.childNodes[0]);
        }
        viewport.setAttribute("content", "initial-scale=" + iscale + ", maximum-scale=" + iscale + ", minimum-scale=" + iscale + ", user-scalable=no");
        var w = getViewWidth(),
            h = getViewHeight(),
            hfz = w / 10 + "px",
            bfz = dpi * 12 + "px";
        BODY.style.fontSize = bfz;
        BODY.style.width = w;
        BODY.style.lineHeight = bfz;
        HTML.style.fontSize = hfz;
        return setPageView;
    }();
    window.onresize = function() {
        setPageView();
    };
    $(function() {
        var mainScroll, goodlistScroll;

        function pullDownAction() {
            setTimeout(function() {
                mainScroll.refresh();
            }, 1E3);
        }

        function pullUpAction() {
            setTimeout(function() {
                console.log("pullUpAction");
                mainScroll.refresh();
            }, 1E3);
        }

        function _loaded() {
            mainScroll = new iScroll("box", {
                scrollbarClass: "",
                useTransition: true,
                topOffset: 0,
                onRefresh: function() {},
                onScrollMove: function() {},
                onScrollEnd: function() {
                    loadImag();
                }
            });
            goodlistScroll = new iScroll("goodlist_box", {
                scrollbarClass: "",
                useTransition: true,
                topOffset: 0,
                onRefresh: function() {},
                onScrollMove: function() {},
                onScrollEnd: function() {
                    loadImag();
                }
            });
            setTimeout(function() {
                $(".out").removeClass('disappear').addClass('appear')
                $('.menu_bar').removeClass('disappear').addClass('appear')
            }, 300);
            setTimeout(function() {
                $("#goodlist").removeClass("active").addClass("hide");
            }, 800);
        }
        setTimeout(function() {
            _loaded();
        }, 300);
        $(D).on("touchstart", ".button", function() {});
        var sliderList = ["#sour", "#priceSlider", "#sweet", "#bitter", "#puckery", "#alcohol"];
        sliderList.forEach(function(r) {
            initSlider(r,$(r).attr('radio'));
        });

        function initSlider(selector,ratio) {
            if ($(selector).length == 0) {
                console.error("Can't find " + selector + " Node");
            } else {
                $(selector).width(getViewWidth() * ratio);
                var option = {
                    "crossable_handles": false,
                    "left_grip_selector": ".leftGrip",
                    "right_grip_selector": ".rightGrip",
                    "value_bar_selector": ".bar",
                    "value_changed_callback": function(cause, leftValue, rightValue) {
                        $(this).parent().find(".leftLabel").text(leftValue);
                        $(this).parent().find(".rightLabel").text(rightValue);
                    }
                };
                if (selector != "#priceSlider") {
                    delete option.right_grip_selector;
                }
                $(selector).nstSlider(option);
            }
        }
//        $(D).on("touchstart", "body", function(){
//            alert()
//            event.preventDefault();
//        })
        $(D).on("touchstart", "body, #top_purpose, #top_price, #top_taste, #menu_quick, #menu_single,.icon_ban,#top_category", function() {
            var targetObj = $(this);
            if ($(this).hasClass("icon_ban")) {
                targetObj.parent().hide();
                window.location.reload();
            } else {
                var thisID = $(this).attr("id");
                event.preventDefault();
                var showPage = function(id) {
                    switch (id) {
                        case "top_category":
                            $(".list_t li").eq(0).trigger("click");
                            break;
                        case "menu_quick":
                            var goodlist_wrap = $("#goodlist"),
                                main_wrap = $("#main");
                            goodlist_wrap.addClass("hide");
//                            $('.menu_bar').removeClass('appear').addClass('disappear')
                            main_wrap.removeClass("hide");
                            if (!main_wrap.hasClass("active")) {
                                main_wrap.addClass("active");
                            }
//                            setTimeout(function() {
//                                $('.menu_bar').removeClass('disappear').addClass('appear')
//                            }, 100);
                            break;
                        case "menu_single":
                            var goodlist_wrap = $("#goodlist"),
                                main_wrap = $("#main");
                            main_wrap.addClass("hide");
//                            $('.menu_bar').removeClass('appear').addClass('disappear')
                            setTimeout(function() {
                                goodlist_wrap.removeClass("hide");
                                if (!goodlist_wrap.hasClass("active")) {
                                    goodlist_wrap.addClass("active");
                                    goodlistScroll.refresh();
                                }
                            }, 0);
//                            setTimeout(function() {
//                                $('.menu_bar').removeClass('disappear').addClass('appear')
//                            }, 100);
                            break;
                        case "top_purpose":
                            console.log(thisID);
                            break;
                        case "top_taste":
                            console.log(thisID);
                            var taste_wrap = $("#taste"),
                                main_wrap = $("#main>div");
                            main_wrap.addClass("blur");
                            taste_wrap.removeClass("hide");
                            if (!taste_wrap.hasClass("active")) {
                                taste_wrap.addClass("active");
                            }
                            break;
                        case "top_price":
                            var price_wrap = $("#price"),
                                main_wrap = $("#main>div");
                            main_wrap.addClass("blur");
                            price_wrap.removeClass("hide");
                            if (!price_wrap.hasClass("active")) {
                                price_wrap.addClass("active");
                            }
                            break;
                    }
                };
                showPage(thisID);
            }
        });
        $(D).on("click", ".list_t li,.mark_ge", function() {
            var mark_wrap = $("#mark"),
                main_wrap = $("#main>div,#goodlist>div");
            main_wrap.addClass("blur");
            mark_wrap.removeClass("hide");
            if (!mark_wrap.hasClass("active")) {
                mark_wrap.addClass("active");
            } else {
                if ($(this).hasClass("mark_ge")) {
                    $("#mark_close").trigger("touchstart");
                    $(".use_tag b").html($(this).text());
                    return;
                }
            }
            mark_page_init();
        });

        function mark_page_init() {
            var marks = $(".mark_ge");
            var marks_parent = $(".mark_cnt");
            var marks_num = marks.length;
            var _num = "";
            marks_num % 3 ? _num = 1 : _num = 0;
            var _num_s = marks_num / 3;
            var _num_max = parseInt(_num_s + _num);
            marks_parent.width(3.5 * _num_max + "rem");
            var mark_cnt_ls1 = $('<div class="mark_cnt_ls"></div>');
            var mark_cnt_ls2 = $('<div class="mark_cnt_ls"></div>');
            var mark_cnt_ls3 = $('<div class="mark_cnt_ls"></div>');
            for (var i = 0, k = marks.length; i < k; i++) {
                var v = marks.eq(i);
                var v_w_num = v.text().length;
                if (v_w_num > 8) {
                    v.css({
                        "font-size": "0.45rem",
                        "line-height": "0.68rem"
                    });
                } else {
                    if (v_w_num > 4) {
                        v.css({
                            "font-size": "0.5rem",
                            "line-height": "1.2rem"
                        });
                    } else {
                        if (v_w_num > 2) {
                            v.css({
                                "font-size": "0.55rem",
                                "line-height": "2.3rem"
                            });
                        } else {
                            v.css({
                                "font-size": "0.9rem",
                                "line-height": "2.19rem"
                            });
                        }
                    }
                }
                if (i < _num_max) {
                    mark_cnt_ls2.append(v);
                } else {
                    if (i > _num_max && i < parseInt(_num + 1 + _num_s * 2)) {
                        mark_cnt_ls1.append(v);
                    } else {
                        mark_cnt_ls3.append(v);
                    }
                }
            }
            marks_parent.html("");
            marks_parent.append(mark_cnt_ls1, mark_cnt_ls2, mark_cnt_ls3);
            var mark_boxScroll = new iScroll("mark_box", {
                scrollbarClass: "",
                useTransition: true,
                lopOffset: 0,
                onRefresh: function() {},
                onScrollMove: function() {},
                onScrollEnd: function() {}
            });
        }
        $(D).on("touchstart", "#taste_confirm_bt,#price_confirm_bt,#price_close,#mark_close,#taste_close", function() {
            var thisID = $(this).attr("id");
            hidePage(thisID);

            function hidePage(id) {
                switch (id) {
                    case "price_close":
                        ;
                    case "price_confirm_bt":
                        var price_wrap = $("#price"),
                            main_wrap = $("#main>div");
                        $(".price_tag b").html(parseInt($(".leftLabel").text()) + "-" + parseInt($(".rightLabel").text()));
                        main_wrap.removeClass("blur");
                        price_wrap.addClass("hide");
                        break;
                    case "mark_close":
                        var mark_wrap = $("#mark"),
                            main_wrap = $("#main>div,#goodlist>div");
                        main_wrap.removeClass("blur");
                        mark_wrap.addClass("hide");
                        break;
                    case "taste_close":
                        ;
                    case "taste_confirm_bt":
                        var taste_wrap = $("#taste"),
                            main_wrap = $("#main>div");
                        main_wrap.removeClass("blur");
                        taste_wrap.addClass("hide");
                        break;
                }
            }
        });
        $(document).on("click", ".add_to_cart", function(e) {
            var targetData = $(this)[0].getBoundingClientRect();
            if (!$(".shopping_cart")[0]) {
                return false;
            }
            var cartData = $(".shopping_cart")[0].getBoundingClientRect();
            var point = {
                dom: $('<span class="add_point"><span>'),
                x: targetData.left,
                y: targetData.top
            };
            var pointDom = point.dom;
            var isMove = 0;
            pointDom.css({
                "left": point.x - 30,
                "top": point.y - 20
            });
            $("body").prepend(point.dom);
            var ori_x = parseInt(pointDom.css("left"));
            var ori_y = parseInt(pointDom.css("top"));
            var moveToCart = function(x, y) {
                if (isMove == 1) {
                    return;
                }
                isMove = 1;
                var sty = $("<style>@-webkit-keyframes goCartFrame {0% {left: " + x + "px; top : " + y + "px; } 50%{ left: " + (x - 70) + "px; top : " + (y - 90) + "px;} 97% {  left: 93%; top : 95%;" + "opacity:1; } 100% { left: 93%; top : 95%;" + "opacity:0;visibility: hidden;} }</style>");
                $("body").append(sty);
                pointDom.removeClass("goCart");
                setTimeout(function() {
                    pointDom.addClass("goCart");
                }, 10);
                setTimeout(function() {
                    isMove = 0;
                    sty.remove();
                    pointDom.remove();
                    var cart_add_1 = $('<em class="cart_add_1">+1</em>');
                    $(".shopping_cart").append(cart_add_1);
                    setTimeout(function() {}, 1E3);
                    $(".shopping_cart b").html($(".shopping_cart b").html() * 1 + 1);
                }, 600);
            };
            moveToCart(ori_x, ori_y);
        });
    });
    $(".scrollLoading").lazyLoad({
        attr: "data-img",
        callback: function() {}
    });
})(window);
